using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class HoaDonDAO
    {
        public int ThemHoaDon(int MaHD, int MaKH, DateTime NgayThanhToan)
        {
            MaHD = 0;
            try
            {
                SqlConnection con = new SqlConnection();
                DataProvider dp = new DataProvider();
                con = dp.ConnectionData();
                con.Open();

                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sp_ThemHoaDon";

                cmd.Parameters.Add("@Ma", SqlDbType.Int);
                cmd.Parameters.Add("@MaKH", SqlDbType.Int);
                cmd.Parameters.Add("@NgayThanhToan", SqlDbType.DateTime);

                cmd.Parameters["@Ma"].Direction = ParameterDirection.Output;
                cmd.Parameters["@MaKH"].Value = MaKH;
                cmd.Parameters["@NgayThanhToan"].Value = NgayThanhToan;

                cmd.ExecuteNonQuery();
                MaHD = Convert.ToInt32(cmd.Parameters["@Ma"].Value);

                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return MaHD;
        }

        public void CapNhatTongTien(int MaHD, Double TongTien)
        {
            try
            {
                SqlConnection con = new SqlConnection();
                DataProvider dp = new DataProvider();
                con = dp.ConnectionData();
                con.Open();

                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sp_CapNhatTongTien";

                cmd.Parameters.Add("@TongTien", SqlDbType.Money);
                cmd.Parameters.Add("@MaHD", SqlDbType.Int);

                cmd.Parameters["@TongTien"].Value = TongTien;
                cmd.Parameters["@MaHD"].Value = MaHD;

                cmd.ExecuteNonQuery();

                con.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
    }
}
